package org.hxyjs.service.impl;

import org.hxyjs.dao.MongodbDao;
import org.hxyjs.dao.UserDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.*;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.AccountService;
import org.hxyjs.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("userService")
public class UserServiceImpl implements UserService, AccountService {
    @Resource
    UserDao userDao;
    @Resource
    MongodbDao mongodbDao;

    @Override
    public DTO<User> getAllUsers() throws Exception {
        List<User> users = userDao.getAllUsers();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(users).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage(), users);
    }

    @Override
    public DTO<User> getUserByUno(String uno) throws Exception {
        User user = userDao.getUserByUno(uno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(uno).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage(), user);
    }

    @Override
    public DTO<User> deleteUser(String uno) throws Exception {
        Integer result = userDao.deleteUser(uno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.DELETEADMINSUCCESS : MessageEnum.DELETEADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<User> updateUser(User user) throws Exception {
        Integer result = userDao.updateUser(user);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<User> addUserByUno(User user) throws Exception {
        user.setUstate("未激活");
        Integer result = userDao.addUserByUno(user);

        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.ADDADMINSUCCESS : MessageEnum.ADDADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage(), user);
    }

    @Override
    public DTO<User> getUsersByPage(PageInfo pageInfo) throws Exception {
        List<User> users = userDao.getUsersByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(users).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage(), users, pageInfo);
    }

    /**
     * 查询MongoDB
     *
     * @param uno
     * @return
     * @throws Exception
     */
    @Override
    public DTO<User> getUsersWithOrders(String uno) throws Exception {
        User user = userDao.getUsersWithOrders(uno);

        List<MongoDbCondition<String, String>> mongoDbConditions = new ArrayList<MongoDbCondition<String, String>>();
        mongoDbConditions.add(new MongoDbCondition<>("_id", uno));
        List<MongoDBDocument> orderFormWithOrderItems = mongodbDao.getListByRegxp(mongoDbConditions, "OrderFormWithOrderItem");

        orderFormWithOrderItems.forEach(orderFormWithOrderItem -> {
            //isPresent表示判断不为空成立则dto为？与：之间的对象
            user.getOrderForms().add((OrderForm) orderFormWithOrderItem.getT());

        });

        MessageEnum messageEnum = Optional.ofNullable(user).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage(), user);
    }

    @Override
    public DTO<User> activate(String uno) throws Exception {
        User user = new User();
        user.setUno(uno);
        user.setUstate("已激活");
        Integer result = userDao.updateUser(user);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = result > 0 ? MessageEnum.UPDATEADMINSUCCESS : MessageEnum.UPDATEADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage());
    }

    @Override
    public DTO<User> lettory(String uno,String giftName) throws Exception {
        User user = userDao.getUserByUno(uno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum = Optional.ofNullable(uno).isPresent() ? MessageEnum.GETADMINSUCCESS : MessageEnum.GETADMINFAIL;
        return new DTO<User>(messageEnum.getCode(), messageEnum.getMessage(), user,giftName);
    }


    @Override
    public DTO<Account> getAccountByAccountName(String accountName) throws Exception {
        DTO<Account> dto = new DTO<Account>();
        Optional<Account> adminOP = Optional.ofNullable(userDao.getAccountByAccountName(accountName));
        dto.setT(adminOP.get());
        MessageEnum messageEnum = adminOP.isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        dto.setCode(messageEnum.getCode());
        dto.setMsg(messageEnum.getMessage());
        return dto;
    }
}
